﻿using Adp;
using Microsoft.AspNetCore.Mvc;
using Yqd.Models;
using System.Linq;
using Yqd.Controllers.Filter;
using Adp.Common;
using System;
using Yqd.Core;
using System.Collections.Generic;
using Yqd.Enm;

namespace Yqd.Controllers
{
    [Area("Yqd")]
    public class WxMySuperviseController : Controller
    {
        YqdContext ctx;

        public WxMySuperviseController(YqdContext ctx)
        {
            this.ctx = ctx;
        }

        [User]
        public ActionResult GetSearch()
        {
            var states = new List<object>();
            states.Add(new { name = "全部", value = "0" });
            states.AddRange(Enum.GetNames(typeof(督导状态)).Select(q => new
            {
                name = q,
                value = Enum.Parse(typeof(督导状态), q)
            }));
            var depts = new List<object>();
            depts.Add(new { name = "全部", value = "" });
            var data = new List<long>();
            data.AddRange(ctx.Yqd_Supervise.Select(it => it.TaskID));
            data.AddRange(ctx.Yqd_Coordinate.Select(it => it.TaskID));
            var years = new List<object>();
            years.Add(new { name = "全部", value = 0 });
            for(var i = 2021; i <= DateTime.Now.Year; i++)
            {
                years.Add(new { name = i + "年", value = i });
            }
            var r = ctx.Yqd_Task.Where(it => data.Contains(it.TaskID)).Select(it => new { value = it.LeaderUnitCode, name = it.LeaderUnitName }).Distinct();
            depts.AddRange(r);
            return this.ToJson(new
            {
                success = true,
                states,
                depts,
                years
            });
        }

        [User]
        public ActionResult GetList()
        {
            var status = Form.GetInt("status");
            var code = Form.GetString("code");
            var year = Form.GetInt("year");
            var key = Form.GetString("key");
            var page = Form.GetInt("page", 1);
            var pagesize = Form.GetInt("pagesize", 0);
            var data = new List<dynamic>();
            data.AddRange(ctx.Yqd_Supervise.ToList().Select(it => new
            {
                it.TaskID,
                it.SuperviseID,
                CoordinateID = 0,
                Tag = it.SuperviseType.ToString(),
                it.Status,
                it.Content,
                it.Instime
            }));
            data.AddRange(ctx.Yqd_Coordinate.ToList().Select(it => new
            {
                it.TaskID,
                SuperviseID = 0,
                it.CoordinateID,
                Tag = "协调",
                it.Status,
                Content = it.Item,
                it.Instime
            }));
            if (status > 0)
            {
                switch ((督导状态)status)
                {
                    case 督导状态.当前待办:
                        data = data.Where(it => it.Tag != "钉办" && it.Status == 办理情况.待交办).ToList();
                        break;
                    case 督导状态.持续跟踪:
                        data = data.Where(it => it.Tag != "钉办" && it.Status == 办理情况.未回复 || it.Status == 办理情况.已回复).ToList();
                        break;
                    case 督导状态.已滞后:
                        data = data.Where(it => it.Tag != "钉办" && it.Status == 办理情况.已滞后).ToList();
                        break;
                    case 督导状态.已经办结:
                        data = data.Where(it => it.Tag != "钉办" && it.Status == 办理情况.已完结).ToList();
                        break;
                    case 督导状态.钉办事项:
                        data = data.Where(it => it.Tag == "钉办").ToList();
                        break;
                }
            }
            else
            {
                data = data.Where(it => it.Tag != "钉办").ToList();
            }
            if (code.Trim() != "")
            {
                var ids = ctx.Yqd_Task.Where(it => it.LeaderUnitCode == code).Select(it=>it.TaskID).ToArray();
                data = data.Where(it => ids.Contains((long)it.TaskID)).ToList();
            }
            if (key.Trim() != "")
            {
                var ids = ctx.Yqd_Task.Where(it => it.TaskName.ToLower().Trim().Contains(key.ToLower().Trim())).Select(it => it.TaskID).ToArray();
                data = data.Where(it => ids.Contains((long)it.TaskID)).ToList();
            }
            if (year > 0)
            {
                data = data.Where(it => it.Instime.Year == year).ToList();
            }
            var tIDs = data.Select(it => it.TaskID).ToArray();
            var tasks = ctx.Yqd_Task.Where(it => tIDs.Contains(it.TaskID)).Select(it => new { it.TaskID, it.TaskName,it.LeaderUnitName, it.TaskStatus }).ToList();
            var person = ctx.Yqd_Person.Where(it => it.PersonType == 5).Select(it => new { it.TaskID, it.PersonName }).ToList();
            data = data.OrderByDescending(it => it.Instime).ToList();
            dynamic t = null;
            var rs = data.ToList().Select((it, index) => new
            {
                Num = index + 1,
                TaskID = it.TaskID.ToString(),
                it.SuperviseID,
                it.CoordinateID,
                (t = tasks.FirstOrDefault(n => n.TaskID == it.TaskID))?.TaskName,
                t.LeaderUnitName,
                PersonName = person.FirstOrDefault(m => m.TaskID == it.TaskID)?.PersonName??"",
                TaskStatus = Enum.GetName(typeof(任务状态), t.TaskStatus),
                DealStatus = it.Tag != "钉办" ? Enum.GetName(typeof(办理情况), it.Status) : "",
                it.Tag,
                Content = it.Content ?? ""
            });
            var totalcount = rs.Count();
            if (pagesize > 0)
            {
                rs = rs.Skip((page-1) * pagesize).Take(pagesize);
            }
            return this.ToJson(new
            {
                success = true,
                totalcount,
                data = rs
            });
        }

        [User]
        public ActionResult GetLogList()
        {
            var status = Form.GetInt("status");
            var code = Form.GetString("code");
            var year = Form.GetInt("year");
            var key = Form.GetString("key");
            var page = Form.GetInt("page", 1);
            var pagesize = Form.GetInt("pagesize", 0);
            var data = new List<dynamic>();
            data.AddRange(ctx.Yqd_Supervise.ToList().Select(it => new
            {
                it.TaskID,
                Tag = it.SuperviseType.ToString(),
                it.Status,
                it.Instime
            }));
            data.AddRange(ctx.Yqd_Coordinate.ToList().Select(it => new
            {
                it.TaskID,
                Tag = "协调",
                it.Status,
                it.Instime
            }));
            if (status > 0)
            {
                switch ((督导状态)status)
                {
                    case 督导状态.当前待办:
                        data = data.Where(it => it.Tag != "钉办" && it.Status == 办理情况.待交办).ToList();
                        break;
                    case 督导状态.持续跟踪:
                        data = data.Where(it => it.Tag != "钉办" && it.Status == 办理情况.未回复 || it.Status == 办理情况.已回复).ToList();
                        break;
                    case 督导状态.已滞后:
                        data = data.Where(it => it.Tag != "钉办" && it.Status == 办理情况.已滞后).ToList();
                        break;
                    case 督导状态.已经办结:
                        data = data.Where(it => it.Tag != "钉办" && it.Status == 办理情况.已完结).ToList();
                        break;
                    case 督导状态.钉办事项:
                        data = data.Where(it => it.Tag == "钉办").ToList();
                        break;
                }
            }
            else
            {
                data = data.Where(it => it.Tag != "钉办").ToList();
            }
            if (code.Trim() != "")
            {
                var ids = ctx.Yqd_Task.Where(it => it.LeaderUnitCode == code).Select(it => it.TaskID).ToArray();
                data = data.Where(it => ids.Contains((long)it.TaskID)).ToList();
            }
            if (key.Trim() != "")
            {
                var ids = ctx.Yqd_Task.Where(it => it.TaskName.ToLower().Trim().Contains(key.ToLower().Trim())).Select(it => it.TaskID).ToArray();
                data = data.Where(it => ids.Contains((long)it.TaskID)).ToList();
            }
            if (year > 0)
            {
                data = data.Where(it => it.Instime.Year == year).ToList();
            }
            var tIDs = data.Select(it => it.TaskID).ToArray();
            var tasks = ctx.Yqd_Task.Where(it => tIDs.Contains(it.TaskID)).Select(it => new { it.TaskID, it.TaskName}).ToList();
            var rs = ctx.Yqd_TaskLog.Where(it => tIDs.Contains(it.TaskID));
            rs = rs.OrderByDescending(it => it.Instime);
            var totalcount = rs.Count();
            if (pagesize > 0)
            {
                rs = rs.Skip((page - 1) * pagesize).Take(pagesize);
            }
            else
            {
                rs = rs.Take(6);
            }
            return this.ToJson(new
            {
                success = true,
                totalcount,
                data = rs.ToList().Select(it => new
                {
                    TaskID = it.TaskID.ToString(),
                    tasks.FirstOrDefault(m => it.TaskID == m.TaskID)?.TaskName,
                    Content = it.Content ?? "",
                    Instime = it.Instime.ToString("yyyy.M.d HH:mm:ss")
                })
            });
        }
    }
}
